package com.meiqiongproperty.yiguanjia.fragment.home.life;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.activity.GenericFragmentActivity;
import com.meiqiongproperty.yiguanjia.databinding.HouseKeepLayoutBinding;
import com.meiqiongproperty.yiguanjia.fragment.BaseFragment;
import com.meiqiongproperty.yiguanjia.fragment.home.adapter.OnItemClickListener;
import com.meiqiongproperty.yiguanjia.fragment.home.adapter.life.LifeDetailListAdapter;
import com.meiqiongproperty.yiguanjia.http.api.ApiService;
import com.meiqiongproperty.yiguanjia.http.interfaces.IHttpCallback;
import com.meiqiongproperty.yiguanjia.http.sendJsonModel.SendLifeDetail;
import com.meiqiongproperty.yiguanjia.httpbean.life.LifeDetailBean;
import com.meiqiongproperty.yiguanjia.utils.Utils;

/**
 * author: Administrator
 * date: 2017/5/5.
 * package:com.meiqiongproperty.yiguanjia.fragment.home.life
 */

public class HouseKeepServiceFragment extends BaseFragment {
    HouseKeepLayoutBinding binding;
    String cateId,title;
    LifeDetailListAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding= DataBindingUtil.inflate(inflater, R.layout.house_keep_layout,container,false);
        binding.setOnclick(this);

        savedInstanceState=getArguments();
        if (savedInstanceState!=null){
            cateId=savedInstanceState.getString("cateId");
            title=savedInstanceState.getString("title");
        }
        binding.navigation.setLeftClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendKeyBackEvent();
            }
        });
        binding.navigation.setTitle(title);

        LinearLayoutManager manager=new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.recyclerLifeDetail.setLayoutManager(manager);

        if (cateId!=null){
            getLifeDetailListData();
        }

        return binding.getRoot();
    }

    /**
     * 获取列表数据
     */
    public void getLifeDetailListData(){
        SendLifeDetail send = new SendLifeDetail();
        send.setToken(Utils.getToken(getActivity()));
        send.setCateId(cateId);
        send.setRoomID(Utils.getRoomID(getActivity()));

        ApiService.getLifeDetailList(getActivity(), new IHttpCallback<LifeDetailBean>() {
            @Override
            public void onNext(final LifeDetailBean lifeDetailBean) {
                adapter=new LifeDetailListAdapter(getActivity(),lifeDetailBean.getService());
                binding.recyclerLifeDetail.setAdapter(adapter);

                adapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        Bundle bundle=new Bundle();
                        bundle.putString("id",lifeDetailBean.getService().get(position).getId());
                        GenericFragmentActivity.start(getActivity(),DailyCleannerFragment.class,bundle);
                    }
                });
            }

            @Override
            public void onError(int code, String message) {

            }
        }, Utils.getRequestBody(send));
    }
}
